/*
struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
    TreeNode(int x) :
            val(x), left(NULL), right(NULL) {
    }
};
*/
/*
// 层次遍历
class Solution {
public:
    char* Serialize(TreeNode *root)
    {
        string s;
        queue<TreeNode*> qt;
        qt.push(root);

        while (!qt.empty())
        {
            // pop operator
            TreeNode *node = qt.front();
            qt.pop();
            // process null node
            if (node == nullptr)
            {
                s.push_back('#');
                s.push_back(',');
                continue;
            }
            // process not null node
            s += to_string(node->val);
            s.push_back(',');
            // push operator
            qt.push(node->left);
            qt.push(node->right);

        }

        char *ret = new char[s.length() + 1];
        strcpy(ret, s.c_str());

        return ret;
    }
    TreeNode* Deserialize(char *str)
    {
        if (str == nullptr) {
            return nullptr;
        }
        // 可用string成员函数
        string s(str);
        if (str[0] == '#') {
            return nullptr;
        }

        // 构造头结点
        queue<TreeNode*> nodes;
        TreeNode *ret = new TreeNode(atoi(s.c_str()));
        s = s.substr(s.find_first_of(',') + 1);
        nodes.push(ret);
        // 根据序列化字符串再层次遍历一遍，来构造树
        while (!nodes.empty() && !s.empty())
        {
            TreeNode *node = nodes.front();
            nodes.pop();
            if (s[0] == '#')
            {
                node->left = nullptr;
                s = s.substr(2);
            }
            else
            {
                node->left = new TreeNode(atoi(s.c_str()));
                nodes.push(node->left);
                s = s.substr(s.find_first_of(',') + 1);
            }

            if (s[0] == '#')
            {
                node->right = nullptr;
                s = s.substr(2);
            }
            else
            {
                node->right = new TreeNode(atoi(s.c_str()));
                nodes.push(node->right);
                s = s.substr(s.find_first_of(',') + 1);
            }
        }
        return ret;
    }
};
*/
// 先序遍历
class Solution {
public:
    char* Serialize(TreeNode *root) {    
        if (!root) {
            return "#";
        }
        string res = to_string(root->val);
        res.push_back(',');
        char* left = Serialize(root->left);
        char* right = Serialize(root->right);
        char* ret = new char[strlen(left)+strlen(right)+res.size()];
        // 如果是string类型，直接用operator += ,这里char* 需要用函数
        strcpy(ret,res.c_str());
        strcat(ret,left);
        strcat(ret,right);
        return ret;
    }
    TreeNode* deseri(char *&s) {
        if (*s == '#') {
            ++s;
            return nullptr;
        }
        // 构造根节点值
        int num = 0;
        while (*s != ',') {
            num = num * 10 + (*s - '0');
            ++s;
        }
        ++s; 
        // 递归构造树
        TreeNode *root = new TreeNode(num);
        root->left = deseri(s);
        root->right = deseri(s);
        return root;
    }

    TreeNode* Deserialize(char *str) {
        return deseri(str);
    }
};
